﻿using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Chess.Common
{
    public class PackageSerializer
    {
        public static byte[] Serialize(Package package)
        {
            var formatter = new BinaryFormatter();
            byte[] buffer;
            using (var stream = new MemoryStream())
            {
                formatter.Serialize(stream, package);
                buffer = stream.GetBuffer();
            }
            return buffer;
        }

        public static Package Deserialize(byte[] bytes)
        {
            var formatter = new BinaryFormatter();
            Package package;
            using(var stream = new MemoryStream(bytes))
            {
                var obj = formatter.Deserialize(stream);
                package = obj as Package;
            }
            return package;
        }


        //public static Package Deserialize(Stream stream)
        //{
        //    var formatter = new BinaryFormatter();
        //    Package package;
        //    var obj = formatter.Deserialize(stream);
        //    package = obj as Package;
            
        //    return package;
        //}
    }
}
